package schoolTec.Prog;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;


public class SchoolTec extends Activity {
	
	private  ArrayList<Host> hosts = new ArrayList<Host>();
	private int count_hosts = 0;	//hier wird die anzahl der hosts geladen
	private ArrayList<ImageView> img_list = new ArrayList<ImageView>();
	private TableLayout tl;
	 
	static final int MENU_EXIT = 0;
	static final int MENU_REFRESH = 1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.table_layout);
		loadData();	        
		
		//baue tabelle
		tl = (TableLayout)findViewById(R.id.myTableLayout);
		for(int i = 0; i < count_hosts; i++) {
			createRow(hosts.get(i).getDescription(),i);
		}
		
		//setze den aktualisierungsbutton
		TableRow tr = new TableRow(this);	//noch ein kleiner trenner zwischen button und letztem eintrag
		tr.setMinimumHeight(20);
		tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		Button bu = new Button(this);
		bu.setOnClickListener(new View.OnClickListener() { 
			public void onClick(View v) { 
				update_states(); 
				} 
			}
		);
		bu.setText("aktualisieren");
		tl.addView(bu,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	}

	/**
	 * Menue erstellen
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		//beenden eintrag
		menu.add(0, MENU_EXIT, 0, R.string.menuBeenden)
		.setShortcut('1', 'x')
		.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		
		//aktualisierungs eintrag
		menu.add(0, MENU_REFRESH, 0, R.string.menuAktualisieren)
		.setShortcut('3', 't')
		.setIcon(android.R.drawable.ic_menu_rotate);
		
		return true;
	}

	/**
	 * erstellt eine Zeile mit einem hostnamen und einem statusbild
	 */
	public void createRow(String title,int num){
		TableRow row = new TableRow(this);
		row.setMinimumHeight(50);
	             
		TextView label = new TextView(this);
		img_list.add(new ImageView(this));
		if(hosts.get(num).isReachable() == 1) img_list.get(num).setImageResource(R.drawable.up);
		if(hosts.get(num).isReachable() == -1) img_list.get(num).setImageResource(R.drawable.down);
		if(hosts.get(num).isReachable() == 0) img_list.get(num).setImageResource(R.drawable.undefined);
		img_list.get(num).setMinimumWidth(45);
		
		label.setText(title);
		label.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
		row.addView(img_list.get(num));
		row.addView(label);
		row.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
		if((num % 2) == 0) row.setBackgroundColor(Color.rgb(40, 40, 40));	//jede zweite zeile farbig abheben
		tl.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case MENU_EXIT:
				finish();
				return true;
			case MENU_REFRESH:
				update_states();
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}

	/**
	 * laed die Dateien in die hosts-Liste
	 */
	public void loadData() {
		hosts.add(new Host("ghs-borken.dyndns.org", 5903, "GHS Borken"));
		hosts.add(new Host("ghs-borken.dyndns.org", 5903, "GHS Borken"));
		hosts.add(new Host("ghs-borken.dyndns.org", 5903, "GHS Borken"));
		hosts.add(new Host("ghs-borken.dyndns.org", 5903, "GHS Borken"));
		hosts.add(new Host("ghs-borken.dyndns.org", 5903, "GHS Borken"));
		hosts.add(new Host("ghs-borken.dyndns.org", 5903, "GHS Borken"));
		hosts.add(new Host("ghs-borken.dyndns.org", 5903, "GHS Borken"));
		hosts.add(new Host("ths-homberg.dyndns.org", 222, "THS Homberg"));
		hosts.add(new Host("ths-homberg.dyndns.org", 35, "Tjener THS Homberg"));
		hosts.add(new Host("ths-homberg.dyndns.org", 22, "Campus THS Homberg"));
		hosts.add(new Host("hellwig-buero.dyndns.org", 28543, "Testzentrum"));
		hosts.add(new Host("school-tec.de", 80, "school-tec.de"));
		hosts.add(new Host("jaja blala", 80, "Zum Test, ist nie erreichbar"));
		hosts.add(new Host("jaja blala", 80, "Zum Test, ist nie erreichbar"));
		count_hosts = hosts.size();
		return;
	}
	
	/**
	 * aktualisiert den Status der in der Liste befindlichen Hosts
	 */
	public void update_states() {
		int status = 0;		
		
		//wenn google.de auf port 80 erreichbar ist, dann existiert eine internetverbindung ^^
		Host google = new Host("google.de", 80, "Google");
		if((google.testhost()) < 1) {
			Toast.makeText(getBaseContext(), "Keine Internetverbindung", Toast.LENGTH_LONG).show();
			return;
		}
		//teste nun der reihe alle anderen hosts auf erreichbarkeit und setze die bilder
		for(int i = 0; i < count_hosts; i++) {
			status = hosts.get(i).testhost();
			if(status == -1) {
				img_list.get(i).setImageResource(R.drawable.down);
				hosts.get(i).setReachable(0);
			}
			else if(status == 0) {
				img_list.get(i).setImageResource(R.drawable.down);
				hosts.get(i).setReachable(0);
			}
			else {
				img_list.get(i).setImageResource(R.drawable.up);
				hosts.get(i).setReachable(1);
			}
			//super.notifyAll(); //nur ein test
		}
		Toast.makeText(getBaseContext(), "aktualisiert", Toast.LENGTH_LONG).show();
		return;
	}
}